package com.device.controller;

import com.device.entity.*;
import com.device.service.ConfigService;
import com.device.service.WebSocketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/config")
public class ConfigController {

    private static final Logger logger = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private ConfigService configService;
    
    @Autowired
    private WebSocketService webSocketService;
    
    // Serial Port Config APIs
    @GetMapping("/serial/{deviceImei}")
    public ResponseEntity<SerialPortConfig> getSerialPortConfig(@PathVariable String deviceImei) {
        SerialPortConfig config = configService.getSerialPortConfig(deviceImei);
        return ResponseEntity.ok(config);
    }
    
    // Polling Config APIs
    @GetMapping("/polling/{deviceImei}")
    public ResponseEntity<PollingConfig> getPollingConfig(@PathVariable String deviceImei) {
        PollingConfig config = configService.getPollingConfig(deviceImei);
        return ResponseEntity.ok(config);
    }
    
    // Network Config APIs
    @GetMapping("/network/{deviceImei}")
    public ResponseEntity<NetworkConfig> getNetworkConfig(@PathVariable String deviceImei) {
        NetworkConfig config = configService.getNetworkConfig(deviceImei);
        return ResponseEntity.ok(config);
    }

    @PostMapping("/totalConfig/{deviceImei}")
    public ResponseEntity<Map<String,Object>> saveTotalConfig(@PathVariable String deviceImei,
                                                                 @RequestBody TotalConfig config) {

        SerialPortConfig originSerialPortConfig = configService.getSerialPortConfig(deviceImei);
        PollingConfig originPollingConfig = configService.getPollingConfig(deviceImei);
        NetworkConfig originNetworkConfig = configService.getNetworkConfig(deviceImei);
        SerialPortConfig serialPortConfig = config.getSerialPortConfig();
        PollingConfig pollingConfig = config.getPollingConfig();
        NetworkConfig networkConfig = config.getNetworkConfig();
        Map<String, Object> changedEntity = new HashMap<>();

        if (null != serialPortConfig && !originSerialPortConfig.equals(serialPortConfig)) {
            configService.saveSerialPortConfig(serialPortConfig);
            changedEntity.put("serialPortConfig", serialPortConfig);
        }
        if (null != pollingConfig && !originPollingConfig.equals(pollingConfig)) {
            configService.savePollingConfig(pollingConfig);
            changedEntity.put("pollingConfig", pollingConfig);
        }
        if (null != networkConfig && !originNetworkConfig.equals(networkConfig)) {
            configService.saveNetworkConfig(networkConfig);
            logger.info("send or not {}", networkConfig);
            changedEntity.put("networkConfig", networkConfig);
        }
        if (!changedEntity.isEmpty()) {
            logger.info("send=================");
            webSocketService.sendConfigUpdate(deviceImei, changedEntity);
        }
        return ResponseEntity.ok(changedEntity);
    }
} 